import org.openqa.selenium.By;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class Element {

    Properties proerties;
    public Element(String path) {
        proerties = new Properties();
        FileInputStream in = null;
        try {
            in = new FileInputStream(path);
            InputStreamReader inputStreamReader = new InputStreamReader(in);//将字节流inputStream转换成字符流inputStreamReader
            proerties.load(inputStreamReader);
//			proerties.load(in);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    /*读取配置文件，根据名称返回内容*/
    public String getConfig(String name) {
        String s =proerties.getProperty(name);
        return s;
    }

    /*根据名称查找元素并返回by*/
    public By parseProFile(String elementName) {
        // 找elementName
//		System.out.println(elementName);
        String by = proerties.getProperty(elementName);
//		System.out.println(by);
        // 根据逗号拆分
        String byType = by.split(",")[0];
        String byValue = by.split(",")[1];
//		System.out.println(byType);
//		System.out.println(byValue);
        switch (byType) {
            case "id":
                return By.id(byValue);
            case "name":
                return By.name(byValue);
            case "class":
                return By.className(byValue);
            case "tag":
                return By.tagName(byValue);
            case "link":
                return By.linkText(byValue);
            case "partiallinktext":
                return By.partialLinkText(byValue);
            case "css":
                return By.cssSelector(byValue);
            case "xpath":
                return By.xpath(byValue);
            default:
                throw new RuntimeException("输入的类型未在程序中定义" + byValue);
        }

    }
}
